package zhr.chapter8;

import java.util.Iterator;
import java.util.LinkedList;

public class Example8_7 {
    public static void main(String[] args) {
        LinkedList<Student> students = new LinkedList<Student>();
        Student stu1 = new Student();
        stu1.setStuName("李雷");
        stu1.setStuAge(21);
        students.add(0, stu1);
        Student stu2 = new Student();
        stu2.setStuName("韩梅梅");
        stu2.setStuAge(22);
        students.add(stu2);
        Student stu3 = new Student();
        stu3.setStuName("张琪");
        stu3.setStuAge(20);
        students.addFirst(stu3);
        students.addLast(stu1);
        System.out.println("当前学生人数为: " + students.size());
        System.out.println("正序遍历: ");
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student stu = (Student) iterator.next();
            System.out.println(stu.printStuInfo());
        }
        students.removeFirstOccurrence(stu1);
        System.out.println("倒序遍历: ");
        iterator = students.descendingIterator();
        while (iterator.hasNext()) {
            Student stu = (Student) iterator.next();
            System.out.println(stu.printStuInfo());
        }
        Student stu4 = new Student();
        stu4.setStuName("刘鑫");
        stu4.setStuAge(24);
        students.set(1, stu4);
        System.out.println("从指定处遍历: ");
        iterator = students.listIterator(1);
        while (iterator.hasNext()) {
            Student stu = (Student) iterator.next();
            System.out.println(stu.printStuInfo());
        }
    }
}
